log using e7 , replace
clear 
set more off
use "charity field1.dta"

gen direct = .
replace direct = 1 if beslutsform == 1
replace direct = 1 if beslutsform == 2
replace direct = 5 if beslutsform == 3
replace direct = 5 if beslutsform == 4
replace direct = 5 if beslutsform == 5
replace direct = 0 if beslutsform == 6
replace direct = 5 if beslutsform == 7
replace direct = 5 if beslutsform == 8
replace direct = 5 if beslutsform == 9

drop if direct == 5
ren direct t 

set seed  358395
sem (d <-  t  wl p)(p <-  t  wl)  , vce(cluster klassnummer)
estat ic
estat teffects 

*** indirect and direct - winning/losing
cap program drop indireff
program indireff, rclass
sem (d <-  t  wl p)(p <-  t  wl)  , vce(cluster klassnummer)
  estat teffects
  mat bi = r(indirect)
  mat bt = r(total)
  return scalar indir  = el(bi,1,3)
  return scalar total  = el(bt,1,3)
end 

set seed 358395
bootstrap r(indir) r(total), reps(1000): indireff
estat bootstrap, percentile bc  

*** indirect and direct - treatment
cap program drop indireff
program indireff, rclass
sem (d <- wl  t  p)(p <- wl  t )   , vce(cluster klassnummer)
  estat teffects
  mat bi = r(indirect)
  mat bt = r(total)
  return scalar indirect  = el(bi,1,3)
  return scalar total  = el(bt,1,3)
end

set seed 358395
bootstrap r(indirect) r(total), reps(1000): indireff
estat bootstrap, percentile bc  

*** randomization check
reg  t  gender interest ideology , robust
log close
